<template>
  <a-modal
    title="编辑故障表"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          style="display: none;"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input v-decorator="['id']"/>
        </a-form-item>

        <a-form-item
          label="关联的设备"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-select style="width: 100%" placeholder="请选择关联的设备" v-decorator="['deviceId', {rules: [{ required: true, message: '请选择关联的设备！' }]}]" >
            <a-select-option v-for="(item,index) in groupCodeList" :key="index" :value="item.code" >{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          label="故障名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入故障名称" v-decorator="['errorName', {rules: [{required: true, message: '请输入故障名称！'}]}]" />
        </a-form-item>

        <a-form-item
          label="故障初次创建时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入故障初次创建时间" v-decorator="['createdAt', {rules: [{required: true, message: '请输入故障初次创建时间！'}]}]" />
        </a-form-item>

        <a-form-item
          label="故障类型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入故障类型" v-decorator="['errorType', {rules: [{required: true, message: '请输入故障类型！'}]}]" />
        </a-form-item>

        <a-form-item
          label="上报网关的编号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入上报网关的编号" v-decorator="['gatewayCode', {rules: [{required: true, message: '请输入上报网关的编号！'}]}]" />
        </a-form-item>

        <a-form-item
          label="故障处理状态（未处理，已处理）"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-radio-group v-decorator="['status',{rules: [{ required: true, message: '请选择故障处理状态（未处理，已处理）！' }]}]" >
            <a-radio-button value="Y">是</a-radio-button>
            <a-radio-button value="N">否</a-radio-button>
          </a-radio-group>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { edit } from '@/api/modular/main/bizexception/bizExceptionManage'

  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        groupCodeList: {}
      }
    },
    methods: {
      edit (record) {
        this.visible = true
        setTimeout(() => {
          this.form.setFieldsValue({
            id: record.id,
            deviceId: record.deviceId,
            errorName: record.errorName,
            createdAt: record.createdAt,
            errorType: record.errorType,
            gatewayCode: record.gatewayCode,
            status: record.status
          })
        }, 100)
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true

        validateFields((errors, values) => {
          if (!errors) {
            edit(values).then((res) => {
              this.confirmLoading = false
              if (res.success) {
                this.$message.success('编辑成功')
                this.handleCancel()
                this.$emit('ok', values)
              } else {
                this.$message.info('编辑失败：' + res.message)
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
